package com.arpa.wms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.ReceiveItemDTO;
import com.arpa.wms.domain.entity.ReceiveItem;
import com.arpa.wms.domain.vo.ReceiveItemVO;
import com.arpa.wms.domain.vo.ReceiveVO;
import com.arpa.wms.service.IReceiveItemService;
import com.arpa.wms.service.IReceiveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 收货明细 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-04
 */
@Api(tags = "收货明细")
@RestController
@RequestMapping(value="/receiveItem", produces = "application/json;charset=UTF-8")
@Log4j2
public class ReceiveItemController {

   @Autowired
   private IReceiveItemService iReceiveItemService;


    @Autowired
    private IReceiveService iReceiveService;

    /**
     * 获取 收货明细 列表
     *
     * @param receiveItemDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 收货明细 列表", notes = "获取 收货明细 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveItemVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(ReceiveItemDTO receiveItemDTO, HttpServletRequest request) {
        try {
            ReceiveItemVO receiveItemVOSum = iReceiveItemService.queryListSum(receiveItemDTO);
            List<ReceiveItemVO> receiveItemVOList = iReceiveItemService.queryList(receiveItemDTO);

            Page returnPage = new Page(receiveItemDTO.getPageNum(),receiveItemDTO.getPageSize());
            returnPage.setRecords(receiveItemVOList);
            returnPage.setTotal(receiveItemVOSum.getTotal());
            returnPage.setSumObject(receiveItemVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取 收货明细与收货登记 列表
     *
     * @param receiveItemDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 收货明细与收货登记 列表", notes = "获取 收货明细与收货登记 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveItemVO.class))
    @NeedLogin
    @GetMapping("/itemWithRegisterList")
    public Result itemWithRegisterList(ReceiveItemDTO receiveItemDTO, HttpServletRequest request) {
        try {
            receiveItemDTO.setGroupCode(UserUtil.getBranchCode());
            ReceiveVO receiveVO = iReceiveService.getVOByCode(receiveItemDTO.getReceiveCode());
            return Result.ok().data(receiveVO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页获取 收货明细与收货登记 列表
     *
     * @param receiveItemDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "分页获取 收货明细与收货登记 列表", notes = "分页获取 收货明细与收货登记 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveItemVO.class))
    @NeedLogin
    @GetMapping("/itemWithRegisterListByPaging")
    public Result itemWithRegisterListByPaging(ReceiveItemDTO receiveItemDTO, HttpServletRequest request) {
        try {
            receiveItemDTO.setGroupCode(UserUtil.getBranchCode());
            ReceiveVO receiveVO = iReceiveService.getVOByPaging(receiveItemDTO);
            return Result.ok().data(receiveVO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 添加 收货明细
     *
     * @param entity ReceiveItem
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 收货明细 ", notes = "添加 收货明细 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(ReceiveItem entity, HttpServletRequest request) {
        try {
            entity.setGroupCode(UserUtil.getBranchCode());
            iReceiveItemService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取收货明细详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取收货明细详情", notes = "获取收货明细详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "收货明细code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveItem.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result receiveItem(@PathVariable String code, HttpServletRequest request) {
        try {
            ReceiveItem entity = iReceiveItemService.getOne(new QueryWrapper<ReceiveItem>().lambda().eq(ReceiveItem::getCode,code).eq(ReceiveItem::getGroupCode, UserUtil.getBranchCode()));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取收货明细详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取收货登记信息", notes = "获取收货登记信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "收货明细code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = ReceiveItem.class))
    @NeedLogin
    @GetMapping(value = "/register/{code}")
    public Result receiveRegisterInfo(@PathVariable String code, HttpServletRequest request) {
        try {
            ReceiveItemVO receiveItemVO = iReceiveItemService.getOneByCode(code);
            return Result.ok().data(receiveItemVO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改收货明细
     *
     * @param entity 收货明细
     * @param request
     * @return
     */
    @ApiOperation(value = "修改收货明细", notes = "修改收货明细")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(ReceiveItem entity,HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iReceiveItemService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 收货明细
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 收货明细 ", notes = "删除 收货明细 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iReceiveItemService.remove(new QueryWrapper<ReceiveItem>().lambda().in(ReceiveItem::getCode,codes));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
